package cn.kgc.controller;

import cn.kgc.entity.User;
import cn.kgc.service.Service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class TestController {
    @Resource
    private Service service;

    @RequestMapping("/login")
    public String Login(String sname, int password, HttpSession session) {
        User user = service.getLog(sname, password);
        session.setAttribute("sessuser", user);
        if (user.getLevel() == 1) {
            return "";
        } else if (user.getLevel() == 2) {
            return "redirect:/teacher.action";
        } else if (user.getLevel() == 3) {

        }
        return "";
    }

    @RequestMapping("/teacher")
    public String Teacher(HttpServletRequest request) {
        List<User> users = service.getAllUser();
        request.setAttribute("users", users);
        return "Teacher";
    }

    @RequestMapping("/search")
    public String Search(User user, HttpServletRequest request) {
        System.out.println(user.getSname());
        System.out.println(user.getYanzhi());
        System.out.println("******");
        List<User> users = service.getSearchUser(user.getSname(), user.getGender(), user.getYanzhi());
        request.setAttribute("users", users);
        return "Teacher";
    }

    @RequestMapping("/Delete")
    public String Delete(int sid, RedirectAttributes ra) {
        String msg = "";
        int i = service.deleteUser(sid);
        if (i == 1) {
            msg = "删除成功";
            ra.addFlashAttribute("msg", msg);
            return "redirect:/teacher.action";
        }
        return "";
    }

    @RequestMapping("/Modify")
    public String Modify(User user, RedirectAttributes ra) {
        String msg = "";
        int i = service.modifyUser(user.getSid(), user.getSname(), user.getAge(), user.getYanzhi(), user.getGender());
        if (i == 1) {
            msg = "修改成功";
            ra.addFlashAttribute("msg", msg);
            return "redirect:/teacher.action";
        }
        return "";
    }

    @RequestMapping("/Add")
    public String Add(User user, RedirectAttributes ra) {
        String msg = "";
        int i = service.addUser(user.getSname(), user.getAge(), user.getGender(), user.getYanzhi());
        if (i == 1) {
            msg = "添加成功";
            ra.addFlashAttribute("msg", msg);
            return "redirect:/teacher.action";
        }
        return "";
    }

    @RequestMapping("/name")
    public void ajax(HttpServletResponse response, String sname) {
        User user = service.ajaxquery(sname);
        String answer = "";
        if (user == null) {
            answer = "滚犊子！";
        } else {
            answer = "自己人！";
        }

        try {
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(answer);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}
